package com.qinghong.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.qinghong.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021000118650438";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEuwIBADANBgkqhkiG9w0BAQEFAASCBKUwggShAgEAAoIBAQCP3GRT4c02fQmhxdzYKkSZ9hK/Tr+9F+K3L1qqVAKbhYe+ixYglbA/EZs6mr3V6WJgenPqm9egRUtyHFwagoJk4qJ6O2/DDITZmVgqsC1mBYpTp/qby7bxEx7Wos5lr2fiEWFhtK24Y4QtBHlNfbA1yMHx5q57Gpm5ZwfdYd8O4pLk8T1J+Cltwbjc+fRJe7Nty2Hmgqz8u5JpW6BMikuoD/3q3Wf6+R+GV4R8sF8s/2QWVCWqCraAVABqsvHgEj+6ZMpFsDPadVUtMWbHJvDkbEBT+fHUyFAuRzI+mNLHm941v0cmEWkwqWLHY4Bw4pwzRsf8xtjwSqRXW4sOTOAhAgMBAAECggEAPKEBl2h+eAxSVt9wp5mIt+pBFGc335WFcvZ+G3K0dp7cCq3LcYgkczAUX6p/fvuQ/mAdIi7zvRP3K+xP1MxT7/ijySfrfiR0iNBbnAIL4AdNuNv+flJok0kfog2TNr+p5Fi21dqzrBYkymQsS0BBVyl4s14pArC/QKfvZJmuzDKGNOyEcC/rY3XkxYUw7mfu8UhLRSvo1imtnsIgxqirDx5KdZ7FT6ieHgVhRcqvGBhDENBRZQrDU7/vhyoZvnLmSkCfoicrF3i4Ut8GMVFLDh1evyQ9Zf2ZlFtIReAQUXkjwV8Bbagk+sTUwOHNsB06eQi+vLcon7QGvbmj4n3zAQKBgQDWywRHItGJP7lKOrTQA+ZhRdE390JcUEFQb3n5Ha+abipRquW3gizHZaq6kdtONAmwjJkti/DdddqhP0AJzynvJpql9IEjW4+EUOuIg2kRUzPFHZ4bFMVnw5/ScN2k2WILwv/yXzl3uVnOHukmin/vP55usxT+GhbfoWvzZ3dI2QKBgQCrdbvvsgdpr5jC6OqCOPhMLQbDmFsjderkbtEowFvOoNc8eIgtzGusohtjhmXMGWfvlr/ONfA/KHlqX+Twg5K9YWpS22i0MU1ndhB4kNyRTu1ltE9870ptwXl4VhafQNoCjDqnAIRaSTXa+QRuH/jqNxEDL+kL5Z6FQoTp9L+EiQKBgQCnGY+9a7gJh3q15dAO2EndOPyYBpMo0ALP+bmxdUfM45GmsUY/KlV91nko5hnv0AoNwIPWbD05fNtWgKdoOjOMasL7AqmIgU24iV+Dd4BAyzLGUdFTjig5HnZyt/2mprthHRYTT8OcgaT89w05P5xh22N9CXL2cAOFfMZHuSGSmQJ/VbqIPVDGZBFoUAwlNPdHLs9UknebGh/bUlwWOEn1nVrOQ24AqOWM8MQc7aZFP8d0MFcvj+toNCwIiSdpUNh4hjgu8T7DG1WLXi+qDJ5hqkXMbQ26sLz0eexCz+g7Z2UFnECBEv8vLfVs+A+KJ4XU70aZOv65hzlICVk5G8FEAQKBgDuuqVgAnTsR8nLkLpk3AwF727OJGTUArWmjpMe9ji+HR22Z+46wo7nHFkK2xCPdkW6Div1E0BCi0bpslxpQvFSOwDibN843YW9Jvh+PuflQyGeUg+kGstW3IbSiTPMhq4SForhMPS2BMkX4p8+H73VnafNoTfds0VEGezxO+lyQ";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4eR+B0bzFqJNm1jSetOYLxDkQd+KnfajsZ2YgIdCnD/yXkcFLIsQdBD+gtjpLza/ELskyQIXOCigt/x0GZ/0MxMZNMDNYA3JB6l6Wi0nPAJav3hjfkOe4ML6R4uuVn1+yf9OI9s36j26O0YtwGbQsLMOPAB4xEtZT3wdZBQCYuB/CFUCYu5brQc0CVlujj70L0XKSQueo7cGI21FBM9P2uF+hdgHij/K6+Mj9Ajw3X25cUBBqXrDG4ce/c+rZkZfuG8cdrHhO4rpBd3RkUCNFvvcO9WAHH2YFOCIhRAHX4Op6UUsGKh3B4wDOzch7Rf6vmRepL4K5E+li/vmF0306wIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://6b99d3d01cc99879.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://order.gulimall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}